using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_CommonCostItemReduce : UIBaseNode
    {

        #region Template Generate,don't modify
        protected class UIB_UIN_CommonCostItemReduce
        {
            #region ObjectBinding Generate 
            public UnityEngine.GameObject commonItem { protected set; get; }
            public UnityEngine.GameObject select { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textNumUse { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnReduce { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonItem", out var __tbv0);
                this.commonItem = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("select", out var __tbv1);
                this.select = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textNumUse", out var __tbv2);
                this.textNumUse = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnReduce", out var __tbv3);
                this.btnReduce = __tbv3;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        UIN_OneItem item = null;
        int itemCid = -1;
        int numCost = -1;
        Action<int, int> callOnReduce = null;

        protected UIB_UIN_CommonCostItemReduce ui { get; set; }
        private UIMsgDispatcher currMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_CommonCostItemReduce();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.item = this.InitChildNode<UIN_OneItem>(this.ui.commonItem);
            this.ui.btnReduce.onClick.AddListener(this.OnClickReduce);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        void OnClickReduce()
        {
            int numCost = Math.Max(0, this.numCost - 1);
            if (numCost != this.numCost)
            {
                this.numCost = numCost;
                this.ViewNumCost(numCost);

                this.callOnReduce?.Invoke(this.itemCid, this.numCost);
            }
        }

        protected override void OnDispose()
        {
            this.item = null;
            this.callOnReduce = null;
        }

        protected override void AddMsgListeners()
        {
            currMsgDispatcher.AddListener<int>(MsgEventType.OneItemChanged, this.OnMsgOneItemChanged);
        }

        void OnMsgOneItemChanged(int cid)
        {
            if (cid == this.itemCid)
            {
                this.ViewNumHave();
            }
        }

        public void Show(int itemCid, int numCost = 1)
        {
            var cfgItem = TableCenter.item.Get(itemCid);
            bool isOkey = this.item.Show(cfgItem);
            if (!isOkey)
            {
                this.itemCid = -1;
                this.numCost = -1;
                this.Hide();
                return;
            }
            this.itemCid = itemCid;
            this.numCost = numCost;

            this.Show();
            this.ViewNumHave();
            this.ViewNumCost(this.numCost);
        }

        public void SetCallOnReduce(Action<int, int> call)
        {
            this.callOnReduce = call;
        }

        void ViewNumHave(bool isActive = true)
        {
            int numHave = ItemModule.Instance.GetNum(this.itemCid);
            this.item.SetCount(numHave, isActive);
        }

        void ViewNumCost(int numCost)
        {
            bool isCost = numCost > 0;
            this.SetSelectActive(isCost);
            if (isCost)
            {
                this.ui.textNumUse.text = numCost.ToString();
            }
        }

        void SetSelectActive(bool active)
        {
            this.ui.select.SetActive(active);
        }
    }
}
